package com.cqs.leetcode;

import com.cqs.leetcode.ds.ArraysUtils;

/**
 * Created by cqs on 2018/4/9.
 */
public class BestTimeBuySellStock {

    public int maxProfit(int[] prices) {
        if (prices == null || prices.length <= 1) return 0;
        int min = prices[0];//至第i个元素时prices最小值
        int max = 0;//至第i个元素时股票最大收益
        for (int i = 1; i < prices.length; i++) {
            max = Math.max(max, prices[i] - min);
            min = Math.min(min, prices[i]);
        }
        return max;
    }

    public static void main(String[] args) {
        int[] prices = ArraysUtils.randInts(10);
        ArraysUtils.print(prices);
        BestTimeBuySellStock stock = new BestTimeBuySellStock();
        int earnings = stock.maxProfit(prices);
        System.out.println("max earnings:" + earnings);
    }
}
